package com.icehand.utils.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串匹配
 * Created by icehand on 2017/5/3.
 */
public class StringMatchUtils {
	/**
	 * 正则匹配
	 * @param regex
	 *            正则表达式
	 * @param str
	 *            匹配的字符串
	 * @return boolean 如果字符串符合regex return true,else return false
	 */
	public static boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	/**
	 * 邮箱匹配
	 * @param str
	 *            匹配的字符串
	 * @return boolean
	 */
	public static boolean isEmail(String str) {
		String regex = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
		return match(regex, str);
	}

	/**
	 * 网址匹配
	 * @param str
	 *            匹配的字符串
	 * @return boolean
	 */
	public static boolean isUrl(String str) {
		String regex = "[a-zA-z]+://[^\\s]";
		return match(regex, str);
	}

	/**
	 * 手机号匹配
	 * @param str
	 *            匹配的字符串
	 * @return boolean
	 */
	public static boolean isChinaPhone(String str) {
		String regex = "^[1][0-9][0-9]{9}$";
		return match(regex, str);
	}
}
